package com.buddy.sds.auth.controller;

import com.buddy.sds.auth.entity.PermissionTree;
import com.buddy.sds.auth.entity.SystemPermission;
import com.buddy.sds.auth.entity.SystemRole;
import com.buddy.sds.auth.service.ISystemPermissionService;
import com.buddy.sds.auth.service.ISystemRoleService;
import com.buddy.sds.common.RestApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth/role")
public class SystemRoleController {

    @Autowired
    private ISystemRoleService systemRoleService;

    @Autowired
    private ISystemPermissionService permissionService;

    /**
     * 获取角色的权限树
     * @return
     */
    @GetMapping("/getRolePermissionTree")
    public RestApiResponse getPermissionTree(@RequestParam("roleId") String roleId) {
        List<PermissionTree> permissionTreeList = permissionService.getPermissionTreeByRole(roleId);
        return new RestApiResponse(0, "", permissionTreeList);
    }
    @PutMapping("/updatePermission")
    public RestApiResponse updatePermission(@RequestParam("roleId") String roleId,@RequestBody List<String> permissions){

        SystemRole role = systemRoleService.getById(roleId);

        if(role == null){

            return new RestApiResponse(-1,"角色不存在",null);
        }

        /*
        if(!role.isRoleModifyable()){

            return new RestApiResponse(-1,"该角色不允许编辑",null);
        }
        */

        //role不可编辑的  暂时权限只能增不能减
        permissionService.updatePermission(roleId, permissions,role.isRoleModifyable());

        return new RestApiResponse(0,"更新成功",null);
    }

    /**
     * 根据角色id获取权限列表
     * @param roleId
     * @return
     */
    @GetMapping("/getRolePermissionList")
    public RestApiResponse getRolePermissionList(@RequestParam("roleId") String roleId){
        List<SystemPermission> permissions = permissionService.getPermissionListByRole(roleId);
        return new RestApiResponse(0,"",permissions);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/deleteRole")
    public RestApiResponse deleteRole(@RequestParam("roleId")String roleId){

        SystemRole role = systemRoleService.getById(roleId);

        if(role != null){
            if(role.isRoleModifyable()){

                systemRoleService.deleteCascade(roleId);

                return new RestApiResponse(0,"",null);
            }else{
                return new RestApiResponse(-1,"该角色不能被删除",null);
            }

        }else{

            return new RestApiResponse(-2,"角色不存在",null);
        }
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/getRoleList")
    public RestApiResponse getRoleList(){

        List<SystemRole> roles = systemRoleService.list();

        return new RestApiResponse(0,"",roles);
    }

    /**
     * 更新角色基本信息
     * @param role
     * @return
     */
    @PutMapping("/updateRole")
    public RestApiResponse updateRole(@RequestBody SystemRole role){

        if(StringUtils.isEmpty(role.getRoleId())){
            return new RestApiResponse(-3,"角色id为空",null);
        }

        if(StringUtils.isEmpty(role.getRoleName())){
            return new RestApiResponse(-3,"角色名称为空",null);
        }

        SystemRole oldRole = systemRoleService.getById(role.getRoleId());

        if(oldRole != null){

            if(!oldRole.isRoleModifyable()){
                return new RestApiResponse(-4,"该角色不能被编辑",null);
            }
        }else{
            return new RestApiResponse(-5,"角色不存在",null);
        }

        if(!systemRoleService.roleExists(role.getRoleName())){

            if(systemRoleService.updateById(role)){

                return new RestApiResponse(0,"更新成功",null);
            }else{

                return new RestApiResponse(-2,"更新失败",null);
            }
        }
        else{
            return new RestApiResponse(-1,"更新失败,存在相同角色名称",null);
        }


    }

    /**
     * 根据角色id获取角色信息
     * @param roleId
     * @return
     */
    @GetMapping("/getRole")
    public RestApiResponse getRole(@RequestParam("roleId")String roleId){

        SystemRole systemRole = systemRoleService.getById(roleId);

        return new RestApiResponse(systemRole == null ? -1 : 0 ,systemRole == null ? "角色不存在":"",systemRole);
    }

    /**
     * 添加角色
     * @param systemRole
     * @return
     */
    @PostMapping("/addRole")
    public RestApiResponse addRole(@RequestBody SystemRole systemRole){

        if(StringUtils.isEmpty(systemRole.getRoleName())){
            return new RestApiResponse(-3,"角色名称为空",null);
        }

        if(!systemRoleService.roleExists(systemRole.getRoleName())){

            //除了内置的管理员 其余角色均可以修改
            systemRole.setRoleModifyable(true);
            if(systemRoleService.save(systemRole)){

                return new RestApiResponse(0,"添加成功",null);
            }else{

                return new RestApiResponse(-2,"添加失败",null);
            }
        }
        else{
            return new RestApiResponse(-1,"添加失败,角色已存在",null);
        }

    }

}
